package tool

import java.io.{File, FilenameFilter}
import akka.stream.Materializer
import akka.stream.scaladsl.Sink
import akka.util.ByteString
import com.sun.org.apache.xerces.internal.impl.xpath.XPath.Step

import javax.inject.Inject
import org.apache.commons.io.FileUtils
import play.api.libs.ws.WSClient
import tool.Pojo._
import tool.Tool.fileCopy
import utils.{CommandExecutor, Utils}
import utils.Implicits._

import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import implicits.Implicits._

import scala.concurrent.Future
import scala.language.postfixOps


/**
 * Created by Administrator on 2019/8/23
 */
class MissionTool(myInInput: MyInInput, myInWorkspaceDir: MyInWorkspaceDir, ws: WSClient, configData: ConfigData)(implicit materializer: Materializer) {

  val baseFile = new File(Utils.rPath, "metabo_base.R")

  def isMulti(cpName: String) = {
    val cpNames = cpName.split("_vs_")
    cpNames.size > 2
  }

  def configStep(steps: List[MyStep], commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                                        cpName: String, myResult: MyInResultDir) = {
    steps.sortBy(_.order).foreach { step =>
      step match {
        case Pojo.Step0 => step0(commandExecutor)
        case Pojo.StepBasic => stepBasic(commandExecutor)
        case Pojo.StepCor => stepCor(commandExecutor)
        case Pojo.StepDignose => stepDiagnose(commandExecutor)
        case StepOplsda(isTest) =>
          if (isTest) {
            stepOplsdaTest(commandExecutor)
          } else {
            stepOplsda(commandExecutor)
          }
        case StepPathway(isTest) =>
          if (isTest) {
            stepPathwayTest(commandExecutor)
          } else {
            stepPathway(commandExecutor)
          }
        case Pojo.StepPca => stepPca(commandExecutor)
        case Pojo.StepPlsda => stepPlsda(commandExecutor)
        case StepPotential(isTest) =>
          if (isTest) {
            stepPotentialTest(commandExecutor)
          } else {
            stepPotential(commandExecutor)
          }
        case StepUni(isTest) =>
          if (isTest) {
            stepUniTest(commandExecutor)
          } else {
            stepUni(commandExecutor)
          }
        case _ =>
      }
    }
    commandExecutor
  }

  def stepBasic(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                  cpName: String, myResult: MyInResultDir) = {
    commandExecutor.exec { () =>
      stepBasicin(myInWorkspace.basicDir)
    }.map { () =>
      Tool.stepBasicinCopy
    }.exec { () =>
      stepBasic1(myInWorkspace.basicInDir.dir01)
    }.map { () =>
      Tool.stepBasic1Copy
    }
  }

  def stepBasicin(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "group_stat.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "z-score_calculate.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "heatmap_z_score_plot.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}  --mc ${myInput.metaColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepBasic1(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "class_sum.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "meta_relative_abundance_plot.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}   --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "meta_relative_abundance_sum_test.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "meta_relative_abundance_sum_plot.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}   --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "metabolite_class_pie_plot.R").unixPath} --i ${myInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}  --mc ${myInInput.metaColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def step0(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                              cpName: String, myResult: MyInResultDir) = {
    commandExecutor.exec { () =>
      step0in(myInWorkspace.dir0)
    }

  }

  def stepPca(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                cpName: String, myResult: MyInResultDir) = {
    commandExecutor.exec { () =>
      stepPca1(myInWorkspace.pcaInDir.dir01)
    }.map { () =>
      Tool.stepPca1Copy
    }.exec { () =>
      stepPca2(myInWorkspace.pcaInDir.dir02)
    }.map { () =>
      Tool.stepPca2Copy
    }

  }

  def stepPca1(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_each_group_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_each_group_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepPlsda(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                  cpName: String, myResult: MyInResultDir) = {
    commandExecutor.exec { () =>
      stepPlsdain(myInWorkspace.plsdaDir)
    }.map { () =>
      Tool.stepPlsdainCopy
    }

  }

  def stepOplsda(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                   cpName: String, myResult: MyInResultDir) = {
    if (!isMulti(cpName)) {
      commandExecutor.exec { () =>
        stepOplsdain(myInWorkspace.oplsdaDir)
      }.map { () =>
        Tool.stepOplsdainCopy
      }
    }
    commandExecutor

  }

  def stepOplsdaTest(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                       cpName: String, myResult: MyInResultDir) = {
    if (!isMulti(cpName)) {
      commandExecutor.exec { () =>
        stepOplsdainTest(myInWorkspace.oplsdaDir)
      }.map { () =>
        Tool.stepOplsdainCopy
      }
    }
    commandExecutor

  }

  def stepPotential(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                      cpName: String, myResult: MyInResultDir) = {
    if (isMulti(cpName)) {
      commandExecutor.exec { () =>
        stepPotentialinMulti(myInWorkspace.potentialDir)
      }.map { () =>
        Tool.stepPotentialinMultiCopy
      }
    } else {
      commandExecutor.exec { () =>
        stepPotentialin(myInWorkspace.potentialDir)
      }.map { () =>
        Tool.stepPotentialinCopy
      }
    }

  }

  def stepPotentialTest(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                          cpName: String, myResult: MyInResultDir) = {
    if (isMulti(cpName)) {
      commandExecutor.exec { () =>
        stepPotentialinMultiTest(myInWorkspace.potentialDir)
      }.map { () =>
        Tool.stepPotentialinMultiCopy
      }
    } else {
      commandExecutor.exec { () =>
        stepPotentialinTest(myInWorkspace.potentialDir)
      }.map { () =>
        Tool.stepPotentialinCopy
      }
    }

  }

  def stepCor(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                cpName: String, myResult: MyInResultDir) = {

    if (configData.cor.isSelfCorExec == "T") {
      commandExecutor.exec {
        stepCor0(myInWorkspace.corInDir.dir01)
      }.map { () =>
        stepCor1Copy
      }
    }


    if (myInInput.corDir.listFiles().nonEmpty) {
      if (configData.cor.isCorExec == "T") {
        commandExecutor.exec { () =>
          stepCor1(myInWorkspace.corInDir.dir01)
        }.map { () =>
          stepCor1Copy
        }
      }

      if (myInInput.confounderFile.length() > 0) {
        if (configData.cor.isParCorExec == "T") {
          commandExecutor.exec { () =>
            stepCor2(myInWorkspace.corInDir.dir02)
          }.map { () =>
            stepCor2Copy
          }
        }

      }
    }

    commandExecutor

  }

  def stepPathway(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                    cpName: String, myResult: MyInResultDir) = {

    if (configData.pathway.isPathwayExec == "T") {
      commandExecutor.exec { () =>
        stepPathwayAna(myInWorkspace.pathwayInDir.dir01)
      }.map { () =>
        stepPathway1Copy
      }
    }

    if (configData.pathway.isIPathExec == "T") {
      commandExecutor.exec { () =>
        stepIPathBefore(myInWorkspace.pathwayInDir.dir02)
      }.flatMap { () =>
        stepIPath(myInWorkspace.pathwayInDir.dir02)
      }.map { () =>
        stepIPathCopy
      }
    }

    if (configData.pathway.isEnrichExec == "T") {
      commandExecutor.exec { () =>
        stepEnrich(myInWorkspace.pathwayInDir.dir03)
      }.map { () =>
        stepPathway3Copy
      }
    }

    commandExecutor
  }

  def stepPathwayTest(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                        cpName: String, myResult: MyInResultDir) = {

    if (configData.pathway.isPathwayExec == "T") {
      commandExecutor.exec { () =>
        stepPathwayAna(myInWorkspace.pathwayInDir.dir01)
      }.map { () =>
        stepPathway1Copy
      }
    }

    if (configData.pathway.isEnrichExec == "T") {
      commandExecutor.exec { () =>
        stepEnrich(myInWorkspace.pathwayInDir.dir03)
      }.map { () =>
        stepPathway3Copy
      }
    }

  }

  def stepDiagnose(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                     cpName: String, myResult: MyInResultDir) = {

    if (configData.diagnoseIsExec == "yes") {

      if (configData.execCv == "yes" && !isMulti(cpName)) {
        commandExecutor.exec { () =>
          stepDiagnoseCvSplit(myInWorkspace.diagnoseDir)
        }
      }

      commandExecutor.exec { () =>
        stepDiagnoseRf(myInWorkspace.diagnoseInDir.rfDir.self)
      }

      if (!isMulti(cpName)) {
        commandExecutor.exec { () =>
          stepDiagnoseMgGb(myInWorkspace.diagnoseInDir.rfDir.mgDir.gbDir.self)
        }.exec { () =>
          stepDiagnoseMgLr(myInWorkspace.diagnoseInDir.rfDir.mgDir.lrDir.self)
        }.exec { () =>
          stepDiagnoseMgRf(myInWorkspace.diagnoseInDir.rfDir.mgDir.rfDIr.self)
        }.exec { () =>
          stepDiagnoseMgRoc(myInWorkspace.diagnoseInDir.rfDir.mgDir.rocDir)
        }

        if (configData.execCv == "yes") {
          commandExecutor.exec { () =>
            stepDiagnoseMgGbCv(myInWorkspace.diagnoseInDir.rfDir.mgDir.gbDir.cvDir)
          }.exec { () =>
            stepDiagnoseMgRfCv(myInWorkspace.diagnoseInDir.rfDir.mgDir.rfDIr.cvDir)
          }.exec { () =>
            stepDiagnoseMgLrCv(myInWorkspace.diagnoseInDir.rfDir.mgDir.lrDir.cvDir)
          }
        }

      }

      commandExecutor.map { () =>
        stepDiagnoseRfCopy
      }.exec { () =>
        stepDiagnoseSvm(myInWorkspace.diagnoseInDir.svmDir.self)
      }

      if (!isMulti(cpName)) {
        commandExecutor.exec { () =>
          stepDiagnoseMgGb(myInWorkspace.diagnoseInDir.svmDir.mgDir.gbDir.self)
        }.exec { () =>
          stepDiagnoseMgLr(myInWorkspace.diagnoseInDir.svmDir.mgDir.lrDir.self)
        }.exec { () =>
          stepDiagnoseMgRf(myInWorkspace.diagnoseInDir.svmDir.mgDir.rfDIr.self)
        }.exec { () =>
          stepDiagnoseMgRoc(myInWorkspace.diagnoseInDir.svmDir.mgDir.rocDir)
        }

        if (configData.execCv == "yes") {
          commandExecutor.exec { () =>
            stepDiagnoseMgGbCv(myInWorkspace.diagnoseInDir.svmDir.mgDir.gbDir.cvDir)
          }.exec { () =>
            stepDiagnoseMgRfCv(myInWorkspace.diagnoseInDir.svmDir.mgDir.rfDIr.cvDir)
          }.exec { () =>
            stepDiagnoseMgLrCv(myInWorkspace.diagnoseInDir.svmDir.mgDir.lrDir.cvDir)
          }
        }

      }

      commandExecutor.map { () =>
        stepDiagnoseSvmCopy
      }.exec { () =>
        stepDiagnoseBoruta(myInWorkspace.diagnoseInDir.borutaDir.self)
      }

      if (!isMulti(cpName)) {
        commandExecutor.exec { () =>
          stepDiagnoseMgGb(myInWorkspace.diagnoseInDir.borutaDir.mgDir.gbDir.self)
        }.exec { () =>
          stepDiagnoseMgLr(myInWorkspace.diagnoseInDir.borutaDir.mgDir.lrDir.self)
        }.exec { () =>
          stepDiagnoseMgRf(myInWorkspace.diagnoseInDir.borutaDir.mgDir.rfDIr.self)
        }.exec { () =>
          stepDiagnoseMgRoc(myInWorkspace.diagnoseInDir.borutaDir.mgDir.rocDir)
        }

        if (configData.execCv == "yes") {
          commandExecutor.exec { () =>
            stepDiagnoseMgGbCv(myInWorkspace.diagnoseInDir.borutaDir.mgDir.gbDir.cvDir)
          }.exec { () =>
            stepDiagnoseMgRfCv(myInWorkspace.diagnoseInDir.borutaDir.mgDir.rfDIr.cvDir)
          }.exec { () =>
            stepDiagnoseMgLrCv(myInWorkspace.diagnoseInDir.borutaDir.mgDir.lrDir.cvDir)
          }
        }

      }

      commandExecutor.map { () =>
        stepDiagnoseBorutaCopy
      }.exec { () =>
        stepDiagnoseIn(myInWorkspace.diagnoseInDir.self)
      }.map { () =>
        stepDiagnoseInCopy
      }

    } else {
      myResult.diagnoseDir.deleteQuietly
    }

    commandExecutor

  }

  def cvPDirCopy(sourceDir: CvPDir, targetDir: CvPDir)(implicit fileNameFilter: FilenameFilter) = {
    sourceDir.self.dirCopyToDir(targetDir.self)
    sourceDir.cvDir.dirCopyToDir(targetDir.cvDir)
  }

  def inDirMgCopy(sourceDir: InDirMg, targetDir: InDirMg)(implicit fileNameFilter: FilenameFilter) = {
    cvPDirCopy(sourceDir.gbDir, targetDir.gbDir)
    cvPDirCopy(sourceDir.lrDir, targetDir.lrDir)
    cvPDirCopy(sourceDir.rfDIr, targetDir.rfDIr)
    sourceDir.rocDir.dirCopyToDir(targetDir.lrDir.self)
  }

  def stepDiagnoseRfCopy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    implicit val fileNameFilter = Tool.fileNameFilter
    fileCopy(myWorkspaceDir.diagnoseInDir.rfDir.self, myResultDir.diagnoseInDir.rfDir.self)
    inDirMgCopy(myWorkspaceDir.diagnoseInDir.rfDir.mgDir, myResultDir.diagnoseInDir.rfDir.mgDir)
  }

  def stepDiagnoseSvmCopy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    implicit val fileNameFilter = Tool.fileNameFilter
    fileCopy(myWorkspaceDir.diagnoseInDir.svmDir.self, myResultDir.diagnoseInDir.svmDir.self)
    inDirMgCopy(myWorkspaceDir.diagnoseInDir.svmDir.mgDir, myResultDir.diagnoseInDir.svmDir.mgDir)
  }

  def stepDiagnoseBorutaCopy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    implicit val fileNameFilter = Tool.fileNameFilter
    fileCopy(myWorkspaceDir.diagnoseInDir.borutaDir.self, myResultDir.diagnoseInDir.borutaDir.self)
    inDirMgCopy(myWorkspaceDir.diagnoseInDir.borutaDir.mgDir, myResultDir.diagnoseInDir.borutaDir.mgDir)
  }

  def stepDiagnoseInCopy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    implicit val fileNameFilter = Tool.fileNameFilter
    fileCopy(myWorkspaceDir.diagnoseInDir.self, myResultDir.diagnoseInDir.self)
  }

  def stepPathway1Copy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    Tool.sameDirCopyWithCurrent(myWorkspaceDir.pathwayInDir.dir01, myResultDir.pathwayInDir.dir01)
  }

  def stepPathway3Copy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    Tool.sameDirCopy(myWorkspaceDir.pathwayInDir.dir03, myResultDir.pathwayInDir.dir03)
  }

  def stepCor1Copy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    Tool.sameDirCopyRecu(myWorkspaceDir.corInDir.dir01, myResultDir.corInDir.dir01)
  }

  def stepCor2Copy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    Tool.sameDirCopyRecu(myWorkspaceDir.corInDir.dir02, myResultDir.corInDir.dir02)
  }

  def stepIPathCopy(implicit myWorkspaceDir: MyInWorkspaceDir, myResultDir: MyInResultDir) = {
    fileCopy(myWorkspaceDir.pathwayInDir.dir02, myResultDir.pathwayInDir.dir02)
  }

  def stepUni(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                cpName: String, myResult: MyInResultDir) = {
    commandExecutor.exec { () =>
      if (isMulti(cpName)) {
        stepUniInMulti(myInWorkspace.uniDir)
      } else {
        stepUniin(myInWorkspace.uniDir)
      }
    }.map { () =>
      Tool.stepUniinCopy
    }

  }

  def stepUniTest(commandExecutor: CommandExecutor)(implicit myInWorkspace: MyInWorkspaceDir, myInputFile: MyInputFile,
                                                    cpName: String, myResult: MyInResultDir) = {
    commandExecutor.exec { () =>
      if (isMulti(cpName)) {
        stepUniInMultiTest(myInWorkspace.uniDir)
      } else {
        stepUniinTest(myInWorkspace.uniDir)
      }
    }.map { () =>
      Tool.stepUniinCopy
    }

  }

  def stepUniInMulti(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "kw_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "mul_diff_calculate.R").unixPath}   --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boxplot_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boxplot_all_plot_with_points.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boxplot_all_plot_with_points_and_significant.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath("myBase")} ${new File(Utils.rPath, "boxplot_all_plot_with_points_and_significant_post_hoc.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepUniInMultiTest(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "kw_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "boxplot_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepUniinTest(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "volcano_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "volcano_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "volcano_plot_with_label.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "boxplot_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |# Rscript ${new File(Utils.rPath, "z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepUniin(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "volcano_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "volcano_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "volcano_plot_with_label.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "uni_diff_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boxplot_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boxplot_all_plot_with_points.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepPotentialin(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_collect.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "venn_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_heatmap.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_heatmap_with_name.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_heatmap_ordered_with_name.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_boxplot_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_boxplot_all_plot_only_outlier.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_fc_and_fdr_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_fc_and_p_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepPotentialinTest(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_collect.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "venn_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |#Rscript ${new File(Utils.rPath, "diff_violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepCor0(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_self_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_self_heatmap.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepCor1(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_upload_cor_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --et ${myInInput.corDir.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_upload_heatmap.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --et ${myInInput.corDir.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepCor2(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_confounder_cor_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --et ${myInInput.corDir.unixPath} --confounder ${myInInput.confounderFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_upload_heatmap.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --et ${myInInput.corDir.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseRf(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "rf_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "rf_top_n.R").unixPath}  --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "rf_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath} --mc ${myInInput.metaColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "rf_plot_top_n.R").unixPath}  --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseMgGb(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "mg_gb.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "gb_roc_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "gb_pr_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseCvSplit(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "cv_split.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath} --config ${myInInput.configFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseMgGbCv(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "gb_cv.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "cv_roc_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "cv_roc_all_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseMgRfCv(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "rf_cv.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "cv_roc_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "cv_roc_all_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseMgLrCv(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "lr_cv.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "cv_roc_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "cv_roc_all_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseMgLr(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "mg_logistic.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "model_roc_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "gb_pr_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseMgRf(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "mg_rf.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "model_roc_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "gb_pr_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseMgRoc(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "mg_roc_calculate.R").unixPath} --i ${myInInput.scaleDataFile.unixPath} --g ${myInInput.groupFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "mg_roc_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath} --i ${myInInput.scaleDataFile.unixPath} --base ${Tool.smallBaseFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepIPathBefore(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "idMapping.R").unixPath}  --base ${baseFile.unixPath} --d ${Tool.databaseDir.unixPath} --g ${myInInput.groupFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepPathwayAna(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "idMapping.R").unixPath}  --base ${baseFile.unixPath} --d ${Tool.databaseDir.unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "kegg_enrich.R").unixPath} --base ${baseFile.unixPath} --d ${Tool.databaseDir.unixPath} --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "bubble_plot.R").unixPath}  --base ${baseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "bubble_plot_without_label.R").unixPath}  --base ${baseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pathway_bar_plot.R").unixPath}  --base ${baseFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pathway_layout_plot.R").unixPath} --base ${baseFile.unixPath} --d ${Tool.databaseDir.unixPath} --config ${myInInput.configFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepEnrich(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "enrich_id_mapping.R").unixPath}  --base ${baseFile.unixPath} --d ${Tool.databaseDir.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "enrich_analysis.R").unixPath} --base ${baseFile.unixPath} --d ${Tool.databaseDir.unixPath} --config ${myInInput.configFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def step91Deal = {
    val dir = new File(myInWorkspaceDir.pathwayInDir.dir01, "Sig_Pathway")
    val contextFile = new File(myInWorkspaceDir.pathwayInDir.dir01, "red_hmdb_id.txt")
    if (contextFile.exists()) {
      val jsDir = new File(Utils.dataDir, "js")
      FileUtils.copyDirectoryToDirectory(jsDir, dir)
      val contextMap = contextFile.lines.drop(1).mapOtherByColumns { columns =>
        (columns(0) -> columns(1))
      }.toMap
      dir.listFiles().foreach { file =>
        if (file.isFile && file.getName.endsWith(".json")) {
          val fileName = file.getName
          val jsonStr = file.str
          val prefix = file.namePrefix
          val htmlFile = new File(dir, s"${prefix}.html")
          val context = contextMap.getOrElse(prefix, "")
          views.html.sigmaTemplate(jsonStr, fileName, context).toString().toFile(htmlFile)
        }
      }
    }

  }

  def stepIPath(workspaceDir: File)(implicit myInput: MyInputFile): Future[Unit] = {
    val startTime = System.currentTimeMillis()
    val nameFile = new File(workspaceDir, "name_map.txt")
    if (!nameFile.exists()) {
      return Future {
        println("error")
      }
    }
    val names = nameFile.lines.trimQuote.selectOneColumn("kegg").filter(_ != "NA").notEmptyLines
    if (names.size == 0) {
      return Future {
        println("error")
      }
    }
    val groupFile = myInInput.groupFile
    val groupSize = groupFile.csvLines.selectOneColumn("classnote").distinct.notEmptyLines.size
    val fcFile = new File(workspaceDir, "fc_color.txt")
    val fcMap = fcFile.lines.lineMap.map { map =>
      val col = map("col")
      (map("kegg"), col)
    }.toMap
    val colorMap = fcMap.map { case (keggId, col) =>
      val webColor = col match {
        case "red" => "#FF0000"
        case "DeepSkyBlue" => "#00bfff"
        case "gold" => "#ffd700"
        case _ => ""
      }
      (keggId, webColor)
    }
    val nameStr = names.map { x =>
      val webColor = colorMap(x)
      List(x, webColor, "W20").mkString("\t")
    }.mkString("\n")
    val mapData = Map(
      "selection" -> nameStr,
      "default_opacity" -> 1.toString,
      "default_width" -> 3.toString,
      "default_radius" -> 7.toString,
      "default_color" -> "#666666",
      "background_color" -> "#ffffff",
      "tax_filter" -> "",
      "export_type" -> "svg",
      "export_dpi" -> 1200.toString,
      "include_microbial" -> 0.toString,
      "whole_pathways" -> 0.toString,
      "include_secondary" -> 0.toString,
      "whole_modules" -> 0.toString,
      "include_antibiotic" -> 0.toString,
      "keep_colors" -> 1.toString,
      "include_metabolic" -> 1.toString,
      "query_reactions" -> 0.toString
    )
    ws.url("https://pathways.embl.de/mapping.cgi").withRequestTimeout(600 seconds).post(mapData).
      flatMap { response =>
        response.status match {
          case 200 => val file = new File(workspaceDir, "iPath3.svg")
            val outputStream = java.nio.file.Files.newOutputStream(file.toPath)
            val sink = Sink.foreach[ByteString] { bytes =>
              outputStream.write(bytes.toArray)
            }
            response.bodyAsSource.runWith(sink).andThen {
              case result =>
                outputStream.close()
                result.get
            }.map { x =>
              println(response)
              println(Utils.getTime(startTime))
            }
          case _ => Future {}
        }
      }.recover {
      case e: Exception =>
        e.printStackTrace()
        e.getMessage.toFile(new File(workspaceDir, "error.csv"))
    }

  }

  def stepDiagnoseSvm(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "svm_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "svm_top_n.R").unixPath}  --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "svm_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "svm_plot_top_n.R").unixPath}  --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepDiagnoseBoruta(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boruta.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boruta_after_diffNames_calculate.R").unixPath}  --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def stepDiagnoseIn(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boruta_venn_calculate.R").unixPath} --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "boruta_venn_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))
  }

  def step0in(workspaceDir: File)(implicit myInput: MyInputFile, cpName: String) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "produce_true_sample_info.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInput.groupFile.unixPath} --cp ${cpName}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "produceColor.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInput.groupFile.unixPath} --cp ${cpName} --sc ${myInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "produce_scale_data.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInput.groupFile.unixPath} --cp ${cpName} --sc ${myInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepPotentialinMulti(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "multi_diff_collect.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "multi_diff_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "multi_diff_z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_heatmap.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_heatmap_with_name.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_z_score_heatmap_ordered_with_name.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --mc ${myInInput.metaColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_boxplot_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_boxplot_all_plot_only_outlier.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "diff_violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepPotentialinMultiTest(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "multi_diff_collect.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "multi_diff_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "multi_diff_z_score_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |#Rscript ${new File(Utils.rPath, "diff_violin_all_plot.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepOplsdain(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_parameter_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_score_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_score_label_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_volcano_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_volcano_plot.R").unixPath} --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_volcano_plot_without_label.R").unixPath} --g ${myInInput.groupFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_permutation_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_arg_calculate.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_permutation_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepOplsdainTest(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_volcano_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_permutation_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "oplsda_arg_calculate.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepPlsdain(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "plsda_all_groups_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "plsda_all_groups_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "plsda_all_groups_label_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "plsda_all_groups_boxplot_first2pcs_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "plsda_all_groups_3d_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

  def stepPca2(workspaceDir: File)(implicit myInput: MyInputFile) = {
    val command =
      s"""
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_all_groups_calculate.R").unixPath} --i ${myInInput.dataFile.unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_all_groups_first2pcs_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_all_groups_first2pcs_label_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_all_groups_boxplot_first2pcs_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath} --config ${myInInput.configFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_all_groups_3d_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_all_groups_variance_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_all_combination_plot.R").unixPath} --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_pc_vs_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
         |${"Rscript".condaPath()} ${new File(Utils.rPath, "pca_pc_vs_labels_plot.R").unixPath}  --g ${myInInput.groupFile.unixPath}   --sc ${myInInput.sampleColorFile.unixPath}
           """.stripMargin
    CommandData(workspaceDir, List(command))

  }

}
